// pages/star/index.js
const app = getApp();
import { PACKAGE_STAR_PAGE, PACKAGE_STORE_PAGE, PACKAGE_ORDER_PAGE, PACKAGE_PRODUCT_PAGE, PACKAGE_ACTIVITY_PAGE, COMMON_PAGE, PACKAGE_SIGNIN_PAGE } from '../../constants/route';
import { PageReadonlyDataMixins, ManageTargetInfoUpdateMixins, UserInfoUpdateMixins, ShareImageMixinsHaveTabbar, PageOssMixins } from '../../behaviors/page';
import { setMangeTargetId, setMangeTargetInfo } from '../../biztools/wex';
import { navTo, showLoading, hideLoading } from '../../common/env';
import { getBrandInfo, getBrandList } from '../../api/brd';
import { getStoreInfo, getStoreList } from '../../api/sto';
import { setCloseWxmpAttentionFlag, getCloseWxmpAttentionFlag } from '../../biztools/wex';
import { TARGET_TYPE } from '../../constants/common';
import { ORDER_TYPE_ENUM } from '../../constants/order';
import { ACTIVITY_MENU_ROUTE } from '../../constants/activity';
import { getConfig } from '../../api/common';

Page({
    mixins: [PageReadonlyDataMixins, ManageTargetInfoUpdateMixins, UserInfoUpdateMixins, ShareImageMixinsHaveTabbar, PageOssMixins],
    data: {
        userConfig: {},
        closeWxmpAttentionFlag: false,
        orderTypeEnmu: ORDER_TYPE_ENUM,
        targetType: TARGET_TYPE,
        preStoreCount: 0,
        activityCreateSelectPopup: {
            show: false,
        },
        loaded: false,
        inited: false,
    },
    onLoad(options) {
    },
    async onShow() {
        const config = await getConfig();
        this.setData({ userConfig: config });
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            this._handleGetBrandInfo();
        }
    },
    _handleResetBrand(manageTargetType, manageTargetId, manageTargetInfo = {}) {
        setMangeTargetId(manageTargetType, manageTargetId);
        setMangeTargetInfo(manageTargetInfo);
        this.setData({ manageTargetType });
        this.setData({ manageTargetId });
        this.setData({ manageTargetInfo });
    },
    async _handleGetBrandInfo() {
        const { loaded } = this.data;
        !loaded && showLoading('加载中...');
        let { manageTargetId, manageTargetType, manageTargetInfo } = this.data;
        let closeWxmpAttentionFlag = getCloseWxmpAttentionFlag();
        this.setData({ closeWxmpAttentionFlag });
        if (manageTargetId) {
            // 品牌
            if (manageTargetType === TARGET_TYPE.brand) {
                manageTargetInfo = await getBrandInfo();
                manageTargetId = manageTargetInfo.brandId || manageTargetId;
            }
            // 店铺
            else if (manageTargetType === TARGET_TYPE.store) {
                manageTargetInfo = await getStoreInfo();
                manageTargetId = manageTargetInfo.storeId || manageTargetId;
            }
        }
        if (!manageTargetInfo.brandId && !manageTargetInfo.storeId) {
            const brandList = await getBrandList() || [];
            const storeList = await getStoreList() || [];
            let idx = -1, idxInfo;
            if (manageTargetType === TARGET_TYPE.brand) {
                idx = brandList.findIndex(i => manageTargetId === i.brandId);
                idxInfo = brandList.length ? brandList[idx + 1] : null;
                if (!brandList.length) {
                    manageTargetType = TARGET_TYPE.store;
                }
            } else if (manageTargetType === TARGET_TYPE.store) {
                idx = storeList.findIndex(i => manageTargetId === i.storeId);
                idxInfo = storeList.length ? storeList[idx + 1] : null;
                if (!storeList.length) {
                    manageTargetType = TARGET_TYPE.brand;
                }
            } else {
                if (brandList.length) {
                    manageTargetType = TARGET_TYPE.brand;
                    idx = brandList.findIndex(i => manageTargetId === i.brandId);
                    idxInfo = brandList.length ? brandList[idx + 1] : null;
                } else if (storeList.length) {
                    manageTargetType = TARGET_TYPE.store;
                    idx = storeList.findIndex(i => manageTargetId === i.storeId);
                    idxInfo = storeList.length ? storeList[idx + 1] : null;
                }
            }
            if (manageTargetType === TARGET_TYPE.brand) {
                manageTargetId = idxInfo ? idxInfo.brandId : '';
            } else if (manageTargetType === TARGET_TYPE.store) {
                manageTargetId = idxInfo ? idxInfo.storeId : '';
            }
            this._handleResetBrand(manageTargetType, manageTargetId, manageTargetInfo);
            if (manageTargetId) {
                manageTargetInfo = await this._handleGetBrandInfo(manageTargetId);
                if (manageTargetType === TARGET_TYPE.brand) {
                    manageTargetId = manageTargetInfo.brandId;
                } else if (manageTargetType === TARGET_TYPE.store) {
                    manageTargetId = manageTargetInfo.storeId;
                }
            }
        }
        this._handleResetBrand(manageTargetType, manageTargetId, manageTargetInfo);
        console.log('Star => 获取到的信息为：', manageTargetInfo);
        if (!loaded) hideLoading(); this.setData({ loaded: true });
        this.setData({ inited: true });
        return manageTargetInfo;
    },
    handleShowActivityCreateSelect() {
        this.setData({
            'activityCreateSelectPopup.show': true,
        });
    },
    handleActivityCreateSelect(e) {
        const type = e.detail;
        const { manageTargetType } = this.data;
        let pageUrl = ACTIVITY_MENU_ROUTE[manageTargetType][type];
        navTo(pageUrl);
    },
    handleNavToEarning() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_STAR_PAGE.brandEarning);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_STORE_PAGE.storeEarning);
        }
    },
    handleNavToOrder(e) {
        const { tab } = e.currentTarget.dataset;
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_ORDER_PAGE.brandOrderList, { tab });
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_ORDER_PAGE.storeOrderList, { tab });
        }
    },
    handleNavToActivity() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_ACTIVITY_PAGE['activityList']);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_ACTIVITY_PAGE['storeActivityList']);
        }
    },
    handleNavToHome() {
        const { manageTargetId, manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(COMMON_PAGE['shareBrand'], {
                brandId: manageTargetId,
                owner: true,
            });
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(COMMON_PAGE['shareStore'], {
                storeId: manageTargetId,
                owner: true,
            });
        }
    },
    handleNavToSpokesman() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_STAR_PAGE.brandSpokesman);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_STORE_PAGE.storeSpokesman);
        }
    },
    handleNavToFans() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_STAR_PAGE.brandFans);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_STORE_PAGE.storeFans);
        }
    },
    handleNavToProduct() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_PRODUCT_PAGE.productList);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_PRODUCT_PAGE.storeProductList);
        }
    },
    handleNavToMoreSetting() {
        const { manageTargetType } = this.data;
        if (manageTargetType === TARGET_TYPE.brand) {
            navTo(PACKAGE_STAR_PAGE.brandMoreSetting);
        } else if (manageTargetType === TARGET_TYPE.store) {
            navTo(PACKAGE_STORE_PAGE.storeMoreSetting);
        }
    },
    handleNavToOpen(e) {
        const type = e.detail;
        navTo(PACKAGE_STAR_PAGE.manageTargetOpen, {
            type,
        });
    },
    handleNavToExchange() {
        const { manageTargetType } = this.data;
        navTo(PACKAGE_STAR_PAGE.manageTargetExchange, {
            tab: manageTargetType,
        });
    },
    handleNavToDistributor() {
        navTo(PACKAGE_STAR_PAGE.brandDistributor);
    },
    handleNavToWxpayApplyment() {
        navTo(PACKAGE_STAR_PAGE.brandWxpayApplyment);  
    },
    handleNavToSignin() {
        navTo(PACKAGE_SIGNIN_PAGE.signinEdit);
    },
    handleNavToAttention() {
        navTo(COMMON_PAGE['webview'], {
            url: this.data.userConfig.aboutUrl,
        });
    },
    handleCloseAttention() {
        this.setData({ closeWxmpAttentionFlag: true });
        setCloseWxmpAttentionFlag();
    },
})
